package com.example.zhangjy.collweather.util;

import android.text.TextUtils;

import com.example.zhangjy.collweather.db.CollWeatherDB;
import com.example.zhangjy.collweather.model.City;
import com.example.zhangjy.collweather.model.County;
import com.example.zhangjy.collweather.model.Province;

/**
 * Created by zhangjy on 2015/8/27.
 */
public class Utility {


    /**
     * 解析和处理服务端返回的省份数据
     * @param db
     * @param response
     * @return
     */
    public synchronized static boolean handleProvinceResponse(CollWeatherDB db,String response) {

        if(!TextUtils.isEmpty(response)) {

            String[] allProvince = response.split(",");
            if(allProvince != null && allProvince.length > 0) {
                for (String p : allProvince) {
                    String[] array = p.split("\\|");
                    Province province = new Province();
                    province.setProvinceCode(array[0]);
                    province.setProvinceName(array[1]);

                    db.saveProvince(province);

                }

                return true;
            }
        }

        return false;
    }

    /***
     * 处理和解析服务端返回的市数据
     * @param db
     * @param response
     * @param ProvinceId
     * @return
     */
    public synchronized static boolean handleCityResponse(CollWeatherDB db,String response,int ProvinceId) {

        if(!TextUtils.isEmpty(response)) {

            String[] allCity = response.split(",");
            if(allCity != null && allCity.length > 0) {
                for (String p : allCity) {
                    String[] array = p.split("\\|");
                    City city = new City();
                    city.setCityCode(array[0]);
                    city.setCityName(array[1]);
                    city.setProvinceId(ProvinceId);

                    db.saveCity(city);


                }

                return true;
            }
        }

        return false;
    }


    /***
     * 处理和解析服务端返回的县数据
     * @param db
     * @param response
     * @param cityId
     * @return
     */
    public synchronized static boolean handleCountyResponse(CollWeatherDB db,String response,int cityId) {

        if(!TextUtils.isEmpty(response)) {

            String[] allCounty = response.split(",");
            if(allCounty != null && allCounty.length > 0) {
                for (String p : allCounty) {
                    String[] array = p.split("\\|");
                    County county = new County();
                    county.setCountyCode(array[0]);
                    county.setCountyName(array[1]);
                    county.setCityId(cityId);

                    db.saveCounty(county);


                }

                return true;
            }
        }

        return false;
    }

}
